package com.ruoyi.program.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.program.domain.WxOptionValue;
import com.ruoyi.program.mapper.WxExerciseMapper;
import com.ruoyi.program.domain.WxExercise;
import com.ruoyi.program.service.IWxExerciseService;

/**
 * 知识库Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-26
 */
@Service
public class WxExerciseServiceImpl implements IWxExerciseService
{
    @Autowired
    private WxExerciseMapper wxExerciseMapper;

    /**
     * 查询知识库
     *
     * @param exerciseId 知识库主键
     * @return 知识库
     */
    @Override
    public WxExercise selectWxExerciseByExerciseId(Long exerciseId)
    {
        return wxExerciseMapper.selectWxExerciseByExerciseId(exerciseId);
    }

    /**
     * 查询知识库列表
     *
     * @param wxExercise 知识库
     * @return 知识库
     */
    @Override
    public List<WxExercise> selectWxExerciseList(WxExercise wxExercise)
    {
        return wxExerciseMapper.selectWxExerciseList(wxExercise);
    }

    /**
     * 新增知识库
     *
     * @param wxExercise 知识库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWxExercise(WxExercise wxExercise)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String username = loginUser.getUsername();

        wxExercise.setUserId(loginUser.getUserId());
        wxExercise.setCreateBy(username);  // 创建人
        wxExercise.setUpdateBy(username);  // 更新人（）
        wxExercise.setUpdateTime(new Date());  // 更新时间
        wxExercise.setCreateTime(DateUtils.getNowDate());
        int rows = wxExerciseMapper.insertWxExercise(wxExercise);
        insertWxOptionValue(wxExercise);
        return rows;
    }

    /**
     * 修改知识库
     *
     * @param wxExercise 知识库
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWxExercise(WxExercise wxExercise)
    {
        wxExercise.setUpdateTime(DateUtils.getNowDate());
        wxExerciseMapper.deleteWxOptionValueByExerciseId(wxExercise.getExerciseId());
        insertWxOptionValue(wxExercise);
        return wxExerciseMapper.updateWxExercise(wxExercise);
    }

    /**
     * 批量删除知识库
     *
     * @param exerciseIds 需要删除的知识库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWxExerciseByExerciseIds(Long[] exerciseIds)
    {
        wxExerciseMapper.deleteWxOptionValueByExerciseIds(exerciseIds);
        return wxExerciseMapper.deleteWxExerciseByExerciseIds(exerciseIds);
    }

    /**
     * 删除知识库信息
     *
     * @param exerciseId 知识库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWxExerciseByExerciseId(Long exerciseId)
    {
        wxExerciseMapper.deleteWxOptionValueByExerciseId(exerciseId);
        return wxExerciseMapper.deleteWxExerciseByExerciseId(exerciseId);
    }

    /**
     * 新增知识选项信息
     *
     * @param wxExercise 知识库对象
     */
    public void insertWxOptionValue(WxExercise wxExercise)
    {
        List<WxOptionValue> wxOptionValueList = wxExercise.getWxOptionValueList();
        Long exerciseId = wxExercise.getExerciseId();
        if (StringUtils.isNotNull(wxOptionValueList))
        {
            List<WxOptionValue> list = new ArrayList<WxOptionValue>();
            for (WxOptionValue wxOptionValue : wxOptionValueList)
            {
                wxOptionValue.setExerciseId(exerciseId);
                list.add(wxOptionValue);
            }
            if (list.size() > 0)
            {
                wxExerciseMapper.batchWxOptionValue(list);
            }
        }
    }
}
